﻿using GPS.MODELS;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using GPS.DataAccessLayer;
using AutoMapper;
using PagedList;
using GPS.ENTITYS;
using GPS.COMMON;
using System.IO;
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
using System.Threading.Tasks;
using System.Web.UI.WebControls;
using Oracle.ManagedDataAccess.Client;
using System.Data;
using Microsoft.AspNet.Identity;
using Newtonsoft.Json;
using GPS.GPS_DAL;
using System.Globalization;

namespace GPS.Controllers.GPS_NghiepVu
{
    public class GPS_PhanCongLichHocController : Controller
    {
        DbContextHelper<GPS_NVContext> _db = Singleton<DbContextHelper<GPS_NVContext>>.Inst;
        public ApplicationUser getCurrentUser()
        {
            ApplicationDbContext dbUser = new ApplicationDbContext();

            string userId = User.Identity.GetUserId();

            ApplicationUser _currentUser = dbUser.Users.Find(userId);

            return _currentUser;
        }

        //
        // GET: /GPS_PhanCongLichHoc/
        public ActionResult Index()
        {
            return View();
        }


        public ActionResult Create()
        {
            string Ma_DVGT = getCurrentUser().MA_TINHTP;
            //KH_MAs
            var khoahocs = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_HOCVIEN_LH(Ma_DVGT);  //_db.Filter<DKH>(o => o.KH_DVGTVT_MA == viewModel.KH_LH_DVGTVT_MA);
            ViewBag.KH_MAs = new SelectList(khoahocs, "KH_MA", "KH_TEN");
            ViewBag.DVGTVT_MAs = _db.GetOne<SDONVIGTVT>(o => o.DVGTVT_MA == Ma_DVGT).DVGTVT_TEN;

            var khoahocAUTos = Singleton<GPS_HocVienDAO>.Inst.GPS_GETDS_KHOAHOC_AUTO(Ma_DVGT);
            ViewBag.KH_MA_AUTOs = new SelectList(khoahocAUTos, "KH_MA", "KH_TEN");
            ViewBag.Ma_DVGT = Ma_DVGT;
            return View();
        }



        public ActionResult Edit()
        {
            return View();
        }

        #region"Ajax"

        // Lưu trữ phương tiện
        [HttpPost]
        public ActionResult CreateDKH_PT(JsonDataDKH_PT dkh_pt)
        {
            string Ma_GTVT = this.getCurrentUser().MA_TINHTP;
            if (dkh_pt != null)
            {
                if (dkh_pt.KH_PT_PT_MAs != null && dkh_pt.KH_PT_PT_MAs.Length > 0)
                {

                    int i = 0;
                    string[] KH_PT_PT_MAs = JsonConvert.DeserializeObject<string[]>(dkh_pt.KH_PT_PT_MAs[0]);

                    foreach (var _key in KH_PT_PT_MAs)
                    {
                        DKH_PT1 _itemInsert = new DKH_PT1();
                        _itemInsert.ID = EFSequence.GetNextValue("SEQ_DKH_PT1_ID");
                        _itemInsert.KH_PT_PT_MA = _key;
                        _itemInsert.KH_PT_DVGTVT_MA = Ma_GTVT;
                        _itemInsert.KH_PT_KH_MA = dkh_pt.KH_PT_KH_MA;

                        _itemInsert.KH_PT_NGAYNHAP = DateTime.Now;
                        _itemInsert.KH_PT_NGUOINHAP = User.Identity.Name;

                        //insert vao bảng DKH_PT1
                        _db.Insert<DKH_PT1>(_itemInsert);


                    }
                }
            }

            //get phương tiện trong khóa học
            var ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_PT_IN_KH(Ma_GTVT, dkh_pt.KH_PT_KH_MA);
            var lstPTinKH = new List<JsonDataDKH_PTinKH>();
            lstPTinKH = GPS_Library.MapDataTableToList<JsonDataDKH_PTinKH>(ds.Tables[1]);

            return Json(new { pts = lstPTinKH }, JsonRequestBehavior.AllowGet);
        }

        //Delete phương tiện

        [HttpPost]
        public ActionResult DeleteDKH_PT(string KH_MA, string PT_MA)
        {
            string Ma_GTVT = this.getCurrentUser().MA_TINHTP;
            Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_DELETE_DKH_PT(Ma_GTVT, KH_MA, PT_MA);

            var ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_PT_IN_KH(Ma_GTVT, KH_MA);
            var lstPTinKH = new List<JsonDataDKH_PTinKH>();
            lstPTinKH = GPS_Library.MapDataTableToList<JsonDataDKH_PTinKH>(ds.Tables[1]);
            return Json(new { pts = lstPTinKH }, JsonRequestBehavior.AllowGet);
        }

        // Lưu trữ giảng viên


        [HttpPost]
        public ActionResult GetGV_IN_NOTIN_KH(string Ma_KH, string Ma_DVGT)
        {
            try
            {

                Ma_DVGT = getCurrentUser().MA_TINHTP;
                JsonDataDKH_GVs dkh_gvs = new JsonDataDKH_GVs();
                dkh_gvs.lstGV_KH_IN = new List<JsonDataDKH_GVinPT>();
                dkh_gvs.lstGV_KH_NOTIN = new List<JsonDataDKH_GVinPT>();
                DataSet ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_NOTIN_KH(Ma_DVGT, Ma_KH, "1");
                if (ds.Tables[0].Rows.Count > 0)
                {
                    dkh_gvs.lstGV_KH_IN = GPS_Library.MapDataTableToList<JsonDataDKH_GVinPT>(ds.Tables[0]);
                }


                if (ds.Tables[1].Rows.Count > 0)
                {
                    dkh_gvs.lstGV_KH_NOTIN = GPS_Library.MapDataTableToList<JsonDataDKH_GVinPT>(ds.Tables[1]).ToList();
                }

              //  dkh_gvs.lstGV_KH_NOTIN=

                return Json(new { gvs = dkh_gvs }, JsonRequestBehavior.AllowGet);
            }
            catch( Exception ex)
            {
                throw new Exception(ex.ToString());
            }


            
        }

        [HttpPost]
        public ActionResult CreateDKH_GV_IN_NOTIN(JsonDataDKH_GV dkh_gv, string isaction)
        {
            JsonDataDKH_GVs dkh_gvs = new JsonDataDKH_GVs();
            dkh_gvs.lstGV_KH_IN = new List<JsonDataDKH_GVinPT>();
            dkh_gvs.lstGV_KH_NOTIN = new List<JsonDataDKH_GVinPT>();

            if (dkh_gv != null)
            {

                if (dkh_gv.KH_GV_GV_MAs != null && dkh_gv.KH_GV_GV_MAs.Length > 0)
                {
                    string Ma_GTVT = this.getCurrentUser().MA_TINHTP;
                    int i = 0;
                    string[] KH_GV_GV_MAs = JsonConvert.DeserializeObject<string[]>(dkh_gv.KH_GV_GV_MAs[0]);

                    foreach (var _key in KH_GV_GV_MAs)
                    {
                        if (isaction == "I")
                        {
                            DKH_GV1 _itemInsert = new DKH_GV1();
                            _itemInsert.ID = EFSequence.GetNextValue("SEQ_DKH_GV1_ID");
                            _itemInsert.KH_GV_GV_MA = _key;
                            _itemInsert.KH_GV_DVGTVT_MA = Ma_GTVT;
                            _itemInsert.KH_GV_KH_MA = dkh_gv.KH_GV_KH_MA;
                            _itemInsert.KH_GV_ACTIVE = "1";
                            _itemInsert.KH_GV_NGAYNHAP = DateTime.Now;
                            _itemInsert.KH_GV_NGUOINHAP = User.Identity.Name;

                            //insert vao bảng DKH_GV1
                            _db.Insert<DKH_GV1>(_itemInsert);


                        }
                        else
                        {
                            _db.Delete<DKH_GV1>(o => o.KH_GV_GV_MA == _key && o.KH_GV_DVGTVT_MA == Ma_GTVT && o.KH_GV_KH_MA == dkh_gv.KH_GV_KH_MA);
                            _db.Delete<DKH_PT_GV1>(o => o.KH_PT_GV_GV_MA == _key && o.KH_PT_GV_DVGTVT_MA == Ma_GTVT && o.KH_PT_GV_KH_MA == dkh_gv.KH_GV_KH_MA);
                            _db.Delete<DKH_LH1>(o => o.KH_LH_GV_MA == _key && o.KH_LH_DVGTVT_MA == Ma_GTVT && o.KH_LH_KH_MA == dkh_gv.KH_GV_KH_MA);
                        }


                    }
                    DataSet ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_NOTIN_KH(Ma_GTVT, dkh_gv.KH_GV_KH_MA, "1");
                    if (ds.Tables[0].Rows.Count > 0)
                    {
                        dkh_gvs.lstGV_KH_IN = GPS_Library.MapDataTableToList<JsonDataDKH_GVinPT>(ds.Tables[0]);
                    }

                    if (ds.Tables[1].Rows.Count > 0)
                    {
                        dkh_gvs.lstGV_KH_NOTIN = GPS_Library.MapDataTableToList<JsonDataDKH_GVinPT>(ds.Tables[1]);
                    }

                }
            }



            return Json(new { gvs = dkh_gvs }, JsonRequestBehavior.AllowGet);
        }

        // Lưu trữ giảng viên
        [HttpPost]
        public ActionResult CreateDKH_GV(JsonDataDKH_GV dkh_gv)
        {
            List<JsonDataDKH_GVinPT> lstDKH_GVinPT = new List<JsonDataDKH_GVinPT>();
            if (dkh_gv != null)
            {
                string strWhere = "";
                if (dkh_gv.KH_GV_GV_MAs != null && dkh_gv.KH_GV_GV_MAs.Length > 0)
                {
                    string Ma_GTVT = this.getCurrentUser().MA_TINHTP;
                    int i = 0;
                    string[] KH_GV_GV_MAs = JsonConvert.DeserializeObject<string[]>(dkh_gv.KH_GV_GV_MAs[0]);

                    foreach (var _key in KH_GV_GV_MAs)
                    {

                        //Insert vào bảng DKH_PT_GV1
                        DKH_PT_GV1 _itemDKH_PT_GV1 = new DKH_PT_GV1();
                        _itemDKH_PT_GV1.ID = EFSequence.GetNextValue("SEQ_DKH_PT_GV1_ID");
                        _itemDKH_PT_GV1.KH_PT_GV_DVGTVT_MA = Ma_GTVT;
                        _itemDKH_PT_GV1.KH_PT_GV_KH_MA = dkh_gv.KH_GV_KH_MA;
                        _itemDKH_PT_GV1.KH_PT_GV_PT_MA = dkh_gv.KH_PT_GV_PT_MA;
                        _itemDKH_PT_GV1.KH_PT_GV_GV_MA = _key;
                        _itemDKH_PT_GV1.KH_PT_GV_ACTIVE = "1";

                        _itemDKH_PT_GV1.KH_PT_GV_NGAYNHAP = DateTime.Now;
                        _itemDKH_PT_GV1.KH_PT_GV_NGUOINHAP = User.Identity.Name;

                        _db.Insert<DKH_PT_GV1>(_itemDKH_PT_GV1);

                        if (i < KH_GV_GV_MAs.Length - 1)
                            strWhere = strWhere + _key + ",";
                        else
                            strWhere = strWhere + _key;



                    }
                    lstDKH_GVinPT = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_PT(Ma_GTVT, dkh_gv.KH_GV_KH_MA, dkh_gv.KH_PT_GV_PT_MA);
                }
            }



            return Json(new { gvs = lstDKH_GVinPT }, JsonRequestBehavior.AllowGet);
        }

        //Xóa giảng viên:

        [HttpPost]
        public ActionResult DeleteDKH_GV(string KH_MA, string GV_MA)
        {
            string _GV_MA = GV_MA.Split('#')[0];
            string _PT_MA = GV_MA.Split('#')[1];
            string Ma_GTVT = this.getCurrentUser().MA_TINHTP;
            Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_DELETE_DKH_GV(Ma_GTVT, KH_MA, _PT_MA, _GV_MA);
            var lstDKH_GVinPT = new List<JsonDataDKH_GVinPT>();
            lstDKH_GVinPT = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_PT(Ma_GTVT, KH_MA, _PT_MA);
            return Json(new { gvs = lstDKH_GVinPT }, JsonRequestBehavior.AllowGet);
        }



        // Lưu trữ học viên
        [HttpPost]
        public ActionResult CreateDKH_HV(JsonDataDKH_HV dkh_hv)
        {
            string Ma_GTVT = this.getCurrentUser().MA_TINHTP;
            List<JsonDataDKH_HVinKH> lstDKH_HVinPT = new List<JsonDataDKH_HVinKH>();
            if (dkh_hv != null)
            {
                string strWhere = "";
                if (dkh_hv.KH_HV_HV_MAs != null && dkh_hv.KH_HV_HV_MAs.Length > 0)
                {

                    int i = 0;
                    string[] KH_HV_HV_MAs = JsonConvert.DeserializeObject<string[]>(dkh_hv.KH_HV_HV_MAs[0]);
                    //string _GV_MA = dkh_hv.KH_HV_GV_MA.Split('#')[0];
                    //string _PT_MA = dkh_hv.KH_HV_GV_MA.Split('#')[1];

                    foreach (var _key in KH_HV_HV_MAs)
                    {
                        DKH_HV1 _itemInsert = new DKH_HV1();
                        _itemInsert.ID = EFSequence.GetNextValue("SEQ_DKH_HV1_ID");
                        _itemInsert.KH_HV_HV_MA = _key;
                        _itemInsert.KH_HV_DVGTVT_MA = Ma_GTVT;
                        _itemInsert.KH_HV_KH_MA = dkh_hv.KH_HV_KH_MA;
                        _itemInsert.KH_HV_PT_MA = dkh_hv.KH_HV_PT_MA;

                        _itemInsert.KH_HV_NGAYNHAP = DateTime.Now;
                        _itemInsert.KH_HV_NGUOINHAP = User.Identity.Name;

                        //insert vao bảng DKH_HV1
                        _db.Insert<DKH_HV1>(_itemInsert);


                        if (i < KH_HV_HV_MAs.Length - 1)
                            strWhere = strWhere + _key + ",";
                        else
                            strWhere = strWhere + _key;



                    }

                    lstDKH_HVinPT = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_HV_IN_PT(Ma_GTVT, dkh_hv.KH_HV_KH_MA, dkh_hv.KH_HV_PT_MA);
                }
            }



            return Json(new { hvs = lstDKH_HVinPT }, JsonRequestBehavior.AllowGet);
        }


        //Xóa học viên:
        [HttpPost]
        public ActionResult DeleteDKH_HV(string KH_MA, string HV_MA)
        {
            string _HV_MA = HV_MA.Split('#')[0];
            string _PT_MA = HV_MA.Split('#')[1];
            string Ma_GTVT = this.getCurrentUser().MA_TINHTP;
            Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_DELETE_DKH_HV(Ma_GTVT, KH_MA, _PT_MA, _HV_MA);
            List<JsonDataDKH_HVinKH> lstDKH_HVinPT = new List<JsonDataDKH_HVinKH>();
            lstDKH_HVinPT = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_HV_IN_PT(Ma_GTVT, KH_MA, _PT_MA);
            return Json(new { hvs = lstDKH_HVinPT }, JsonRequestBehavior.AllowGet);
        }


        //

        [HttpPost]
        public ActionResult getPT_IN_KH_MAs(string Ma_KH, string Ma_DVGT)
        {
            //get phương tiện trong khóa học
            Ma_DVGT = getCurrentUser().MA_TINHTP;
            var ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_PT_IN_KH(Ma_DVGT, Ma_KH);
            var lstPTinKH = new List<JsonDataDKH_PTinKH>();
            lstPTinKH = GPS_Library.MapDataTableToList<JsonDataDKH_PTinKH>(ds.Tables[1]);

            var lstKH = GPS_Library.MapDataTableToList<JsonDataDKH_PTinKH>(ds.Tables[0]);

            return Json(new { pts = lstPTinKH, khs = lstKH }, JsonRequestBehavior.AllowGet);
        }


        [HttpPost]
        public ActionResult getGPS_GET_PT_NOTIN_KHs(string Ma_KH, string Ma_DVGT)
        {
            //get phương tiện trong khóa học
            Ma_DVGT = getCurrentUser().MA_TINHTP;
            var lstPT = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_PT_NOTIN_KH(Ma_DVGT, Ma_KH);
            var districts = lstPT.Select(a => "<option value='" + a.PT_MA + "' >" + a.PT_MA + "</option>");
         //   (a.PT_HINHANH != null ? " style='background-size: 30px 30px;background-repeat: no-repeat; background:url( data:image/jpeg;base64," + Convert.ToBase64String(a.PT_HINHANH) + ")'" : "") + ">" + a.PT_MA + "</option>");

            return Content(string.Join("", districts));

        }


        [HttpPost]
        public ActionResult getGPS_GET_GV_NOTIN_PTs(string Ma_DVGT, string Ma_KH, string PT_MA)
        {

            Ma_DVGT = getCurrentUser().MA_TINHTP;
            var lstGV_KH_IN = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_NOTIN_PT(Ma_DVGT, Ma_KH, PT_MA);
            // style="background:url( data:image/jpeg;base64,@Convert.ToBase64String(electedOfficial.Picture) )"
          //  var districts = lstGV_KH_IN.Select(a => "<option value='" + a.GV_MA + "' style=' background-size: 30px 30px;background-repeat: no-repeat; background:url( data:image/jpeg;base64," + Convert.ToBase64String(a.GV_ANHCD) + ")'>" + a.GV_HODEM + " " + a.GV_TEN + " - CMND:" + a.GV_SOCMT + " - Hạng ĐT:" + a.GV_HANGDT_MA + "</option>");
            var districts = lstGV_KH_IN.Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HODEM + " " + a.GV_TEN + " - CMND:" + a.GV_SOCMT + " - Hạng ĐT:" + a.GV_HANGDT_MA + "</option>");
            return Content(string.Join("", districts));
        }


        [HttpPost]
        public ActionResult getKH_MAs(string ma_DVGT)
        {
            var districts = _db.Filter<DKH>(d => d.KH_DVGTVT_MA == ma_DVGT).Select(a => "<option value='" + a.KH_MA + "'>" + a.KH_TEN + "</option>");

            return Content(string.Join("", districts));
        }

        [HttpPost]
        public ActionResult getGV_MAs(string Ma_DVGT, string ma_KH)
        {
            var lstGV_KH_IN = new List<JsonDataDKH_GVinPT>();
            Ma_DVGT = getCurrentUser().MA_TINHTP;
            //  var _HANGDT_RANK = _db.GetOne<DKH>(o => o.KH_MA == ma_KH).KH_HANGDT_RANK;
            DataSet ds = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_NOTIN_KH(Ma_DVGT, ma_KH, "0");
            if (ds.Tables[0].Rows.Count > 0)
            {
                lstGV_KH_IN = GPS_Library.MapDataTableToList<JsonDataDKH_GVinPT>(ds.Tables[0]);
            }

            // style="background:url( data:image/jpeg;base64,@Convert.ToBase64String(electedOfficial.Picture) )"
            //    var districts = lstGV_KH_IN.Select(a => "<option value='" + a.GV_MA + "' style='background-size: 30px 30px;background-repeat: no-repeat;background:url( data:image/jpeg;base64," + a.GV_ANHCD_URL + ")'>" + a.GV_HOTEN + " - CMND:" + a.GV_SOCMT + " - Hạng ĐT:" + a.GV_HANGDT_MA + "</option>");

            var districts = lstGV_KH_IN.Select(a => "<option value='" + a.GV_MA + "'>" + a.GV_HOTEN + " - CMND:" + a.GV_SOCMT + " - Hạng ĐT:" + a.GV_HANGDT_MA + "</option>");
            return Content(string.Join("", districts));
        }

        [HttpPost]
        public ActionResult getPT_MAs(string Ma_DVGT)
        {
            Ma_DVGT = getCurrentUser().MA_TINHTP;

            var districts = _db.Filter<DPT>(d => d.PT_DVGTVT_MA == Ma_DVGT).Select(a => "<option value='" + a.PT_BIENSO + "'> " + a.PT_BIENSO + "</option>");
            //     (a.PT_ANHXE != null ? " style='background-size: 30px 30px;background-repeat: no-repeat;background:url( data:image/jpeg;base64," + Convert.ToBase64String(a.PT_ANHXE) + ")'" : "") + ">" + a.PT_BIENSO + "</option>");

            return Content(string.Join("", districts));
        }




        [HttpPost]
        public ActionResult getGPS_GET_HV_NOTIN_PTs(string Ma_DVGT, string Ma_KH, string PT_MA)
        {
            Ma_DVGT = getCurrentUser().MA_TINHTP;
            var ListHVs = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_HV_NOTIN_PT(Ma_DVGT, Ma_KH, PT_MA);

            var districts = ListHVs.Select(a => "<option value='" + a.HV_MA + "'>" + a.HV_HODEM + " " + a.HV_TEN + "</option>"); ;

            //  return Json(ListHVs);
            return Content(string.Join("", districts));
        }


        //getHV_BY_KH_MAs

        [HttpPost]
        public ActionResult getHV_BY_KH_MAs(string Ma_KH)
        {
            List<SelectItemDHV> ListHVs = new List<SelectItemDHV>();
            var hvs = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_HV_BY_KH_MA(Ma_KH);
            hvs.ForEach(o => ListHVs.Add(new SelectItemDHV { HV_MA = o.HV_MA, HV_HOTEN = o.HV_HODEM + " " + o.HV_TEN }));
            var districts = ListHVs.Select(a => "<option value='" + a.HV_MA + "'>" + a.HV_HOTEN + "</option>"); ;

            //  return Json(ListHVs);
            return Content(string.Join("", districts));
        }

        //  List<JsonDataDKH_HVinKH> lstDKH_GVinPT = new List<JsonDataDKH_HVinKH>();
        //getHV_BY_KH_MAs
        public ActionResult GPS_GET_HV_IN_PT(string Ma_KH, string Ma_GV)
        {
            string _GV_MA = Ma_GV.Split('#')[0];
            string _PT_MA = Ma_GV.Split('#')[1];

            string Ma_DVGT = getCurrentUser().MA_TINHTP;
            List<JsonDataDKH_HVinKH> lstDKH_HVinPT = new List<JsonDataDKH_HVinKH>();
            lstDKH_HVinPT = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_HV_IN_PT(Ma_DVGT, Ma_KH, _GV_MA);

            return Json(new { hvs = lstDKH_HVinPT }, JsonRequestBehavior.AllowGet);
        }

        public ActionResult GPS_GET_GV_IN_PT(string Ma_KH, string Ma_PT)
        {
            JsonDataDKH_GV_HV_IN_PT gv_hv_pts = new JsonDataDKH_GV_HV_IN_PT();
            gv_hv_pts.lstGV_IN_PT = new List<JsonDataDKH_GVinPT>();
            gv_hv_pts.lstHV_IN_PT = new List<JsonDataDKH_HVinKH>();

            string Ma_DVGT = getCurrentUser().MA_TINHTP;

            gv_hv_pts.lstGV_IN_PT = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_GV_IN_PT(Ma_DVGT, Ma_KH, Ma_PT);
            gv_hv_pts.lstHV_IN_PT = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_HV_IN_PT(Ma_DVGT, Ma_KH, Ma_PT);

            return Json(new { gvs = gv_hv_pts }, JsonRequestBehavior.AllowGet);
        }


        #endregion

        #region "Phương tiện AUTO"

        //getGPS_GET_PT_AUTO_NOTIN_KHs:Ma_KH,Ma_DVGT
        [HttpPost]
        public ActionResult getGPS_GET_PT_AUTO_NOTIN_KHs(string Ma_KH, string Ma_DVGT)
        {
            //get phương tiện trong khóa học
            Ma_DVGT = getCurrentUser().MA_TINHTP;
            var lstPT = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_PT_AUTO_NOTIN_KH(Ma_DVGT, Ma_KH);
            var districts = lstPT.Select(a => "<option value='" + a.PT_MA + "'> " + a.PT_MA + "</option>");
            //   (a.PT_HINHANH != null ? " style='background-size: 30px 30px;background-repeat: no-repeat;background:url( data:image/jpeg;base64," + Convert.ToBase64String(a.PT_HINHANH) + ")'" : "") + ">" + a.PT_MA + "</option>");

            return Content(string.Join("", districts));

        }



        //getPT_AUTO_IN_KH_MAs:Ma_KH,Ma_DVGT
        [HttpPost]
        public ActionResult getPT_AUTO_IN_KH_MAs(string Ma_KH, string Ma_DVGT)
        {
            //get phương tiện trong khóa học
            Ma_DVGT = getCurrentUser().MA_TINHTP;
            var lstPT = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_PT_AUTO_IN_KH(Ma_DVGT, Ma_KH);

            return Json(new { pts = lstPT }, JsonRequestBehavior.AllowGet);
        }


        //CreateDKH_PT_AUTO:KH_PT_KH_MA,KH_PT_PT_MAs
        [HttpPost]
        public ActionResult CreateDKH_PT_AUTO(JsonDataDKH_PT_AUTO dkh_pt)
        {
            string Ma_GTVT = this.getCurrentUser().MA_TINHTP;
            if (dkh_pt != null)
            {
                if (dkh_pt.KH_PT_AUTO_KH_MAs != null && dkh_pt.KH_PT_AUTO_KH_MAs.Length > 0)
                {

                    int i = 0;
                    string[] KH_PT_PT_MAs = JsonConvert.DeserializeObject<string[]>(dkh_pt.KH_PT_AUTO_KH_MAs[0]);

                    foreach (var _key in KH_PT_PT_MAs)
                    {
                        DKH_PT_AUTO _itemInsert = new DKH_PT_AUTO();
                        _itemInsert.ID = EFSequence.GetNextValue("SEQ_DKH_PT_AUTO_ID");
                        _itemInsert.KH_PT_AUTO_PT_BIENSO = _key;
                        _itemInsert.KH_PT_AUTO_PT_MA = _key;
                        _itemInsert.KH_PT_AUTO_DVGTVT_MA = Ma_GTVT;
                        _itemInsert.KH_PT_AUTO_KH_MA = dkh_pt.KH_PT_AUTO_KH_MA;
                        _itemInsert.KH_PT_AUTO_SOHV = dkh_pt.KH_PT_AUTO_SOHV;
                        _itemInsert.KH_PT_AUTO_MOTA = dkh_pt.KH_PT_AUTO_MOTA;

                        _itemInsert.KH_PT_AUTO_NGAYNHAP = DateTime.Now;
                        _itemInsert.KH_PT_AUTO_NGUOINHAP = User.Identity.Name;

                        //insert vao bảng DKH_PT_AUTO
                        _db.Insert<DKH_PT_AUTO>(_itemInsert);


                    }
                }
            }

            //get phương tiện AUTO trong khóa học
            var lstPT = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_PT_AUTO_IN_KH(Ma_GTVT, dkh_pt.KH_PT_AUTO_KH_MA);

            return Json(new { pts = lstPT }, JsonRequestBehavior.AllowGet);
        }

        //DeleteDKH_PT_AUTO:id
        //Xóa học viên:
        [HttpPost]
        public ActionResult DeleteDKH_PT_AUTO(string KH_MA, string id)
        {
            decimal _id = decimal.Parse(id);
            _db.Delete<DKH_PT_AUTO>(o => o.ID == _id);
            string Ma_GTVT = this.getCurrentUser().MA_TINHTP;
            //get phương tiện AUTO trong khóa học
            var lstPT = Singleton<GPS_DAL.GPS_HocVienDAO>.Inst.GPS_GET_PT_AUTO_IN_KH(Ma_GTVT, KH_MA);

            return Json(new { pts = lstPT }, JsonRequestBehavior.AllowGet);
        }



        #endregion

    }

    public class JsonDateDKH_PT_AUTO
    {
        public string KH_MA { get; set; }
        public string KH_TEN { get; set; }
        public decimal? ID { get; set; }
        public string PT_MA { get; set; }
        public decimal? PT_SOHV { get; set; }
       // public byte[] PT_HINHANH { get; set; }
        public string PT_HINHANH_URL
        {
            get
            {
                //String img64 = Convert.ToBase64String(this.PT_HINHANH);
                //string img64Url = string.Format("data:image/" + this.PT_HINHANH + ";base64,{0}", img64);
                string imageBase64Data = string.Empty;
                //if (this.PT_HINHANH != null)
                //    imageBase64Data = Convert.ToBase64String(this.PT_HINHANH);
                //string imageDataURL = string.Format("data:image/jpg;base64,{0}", imageBase64Data);
                return imageBase64Data;
            }
        }
    }

    public class JsonDataDKH_PT_AUTO
    {
        public string KH_PT_DVGTVT_MA { get; set; }
        public string KH_PT_AUTO_KH_MA { get; set; }
        public decimal KH_PT_AUTO_SOHV { get; set; }
        public string KH_PT_AUTO_MOTA { get; set; }

        public string[] KH_PT_AUTO_KH_MAs { get; set; }
    }

    public class JsonDataDKH_PT
    {
        public string KH_PT_DVGTVT_MA { get; set; }
        public string KH_PT_KH_MA { get; set; }
        public string[] KH_PT_PT_MAs { get; set; }
    }

    public class JsonDataDKH_GV
    {
        public string KH_GV_DVGTVT_MA { get; set; }
        public string KH_GV_KH_MA { get; set; }

        public string KH_PT_GV_PT_MA { get; set; }

        public string[] KH_GV_GV_MAs { get; set; }
    }

    public class JsonDataDKH_HV
    {
        public string KH_HV_DVGTVT_MA { get; set; }
        public string KH_HV_KH_MA { get; set; }
        public string KH_HV_GV_MA { get; set; }
        public string KH_HV_PT_MA { get; set; }
        public string[] KH_HV_HV_MAs { get; set; }
    }

    public class JsonDataDKH_GVinPT
    {
        public string GV_MA { get; set; }
        public string GV_HOTEN { get; set; }
        public string GV_SOCMT { get; set; }
        public string GV_HANGDT_MA { get; set; }
        public string GV_HTDTAO_MA { get; set; }
        public string PT_MA { get; set; }
        //    public byte[] GV_ANHCD { get; set; }
        public string GV_ANHCD_URL
        {
            get
            {
                //String img64 = Convert.ToBase64String(this.GV_ANHCD);
                //string img64Url = string.Format("data:image/" + this.GV_ANHCD + ";base64,{0}", img64);
                string imageBase64Data = string.Empty;
                //if (this.GV_ANHCD != null)
                //    imageBase64Data = Convert.ToBase64String(this.GV_ANHCD);
                //  string imageDataURL = string.Format("data:image/jpg;base64,{0}", imageBase64Data);
                return imageBase64Data;
            }
        }

    }

    public class JsonDataDKH_GV_HV_IN_PT
    {
        public List<JsonDataDKH_HVinKH> lstHV_IN_PT
        { get; set; }

        public List<JsonDataDKH_GVinPT> lstGV_IN_PT
        { get; set; }

    }
    public class JsonDataDKH_GVs
    {
        public List<JsonDataDKH_GVinPT> lstGV_KH_IN
        { get; set; }

        public List<JsonDataDKH_GVinPT> lstGV_KH_NOTIN
        { get; set; }

    }

    public class JsonDataDKH_HVinKH
    {
        public string HV_MA { get; set; }
        public string HV_HOTEN { get; set; }

        public string HV_NGAYSINH { get; set; }

        public string HV_HANGDT_MA { get; set; }

        public string HV_GIOITINH { get; set; }
        public string HV_SOCMT { get; set; }

        public string HV_PT_MA { get; set; }
    }

    public class JsonDataDKH_PTinKH
    {
        public string KH_MA { get; set; }
        public string KH_TEN { get; set; }
        public string KH_HANGDT { get; set; }
        public string KH_HANGGPLX { get; set; }
        public string KH_SOLUONGHOVIEN_1_PT { get; set; }
        public string KH_NGAY_KG { get; set; }
        public string KH_NGAY_KT { get; set; }

        public string KH_PT_GV_MA { get; set; }
        public string GV_HOTEN { get; set; }

        public string PT_MA { get; set; }
       // public byte[] PT_HINHANH { get; set; }
        public string PT_HINHANH_URL
        {
            get
            {
                //String img64 = Convert.ToBase64String(this.PT_HINHANH);
                //string img64Url = string.Format("data:image/" + this.PT_HINHANH + ";base64,{0}", img64);
                string imageBase64Data = string.Empty;
                //if (this.PT_HINHANH != null)
                //    imageBase64Data = Convert.ToBase64String(this.PT_HINHANH);
                //string imageDataURL = string.Format("data:image/jpg;base64,{0}", imageBase64Data);
                return imageBase64Data;
            }
        }

    }
}